package chapter19.homework2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;

public class JavaIterator {
    ArrayList<Student> list = null;
    public JavaIterator(){
        Student[] students = new Student[3];
        list = new ArrayList<>();
        students[0] =  new Student("小包",20);
        students[1] =  new Student("小红",25);
        students[2] =  new Student("小黑",30);
        for (int i = 0; i < 3; i++) {
            list.add(students[i]);
        }
    }

    public void display(){
        Iterator<Student> t=list.iterator();
        System.out.println("遍历获得的原始数据：");
        while(t.hasNext()){
            Student student=t.next();
            System.out.println("姓名："+student.getName()+"今年"+student.getAge()+"岁");
        }
        Collections.sort(list);
        Iterator<Student> it = list.iterator();
        System.out.println("========================================");
        System.out.println("按年龄从大到小排序：");
        while(it.hasNext()){
            Student student=it.next();
            System.out.println("姓名："+student.getName()+"今年"+student.getAge()+"岁");
        }
    }
}
